﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;
using System.Windows.Input;
using System.Security;
using System.Windows.Controls;

namespace MetaTracker.ViewModel
{
    internal class UserSelectionTabViewModel : TrackerBaseViewModel
    {
        #region Data and Properties

        public string LogonButtonText
        {
            get
            {
                if (IsLoggedOn)
                {
                    return "_Log Off ";
                }
                else
                {
                    return "_Log In ";
                }
            }
        }

        public SecureString Password 
        {
            get
            {
                return tracker.Password;
            }
            set
            {
                tracker.Password = value;
            }
        }


        #endregion //Data and Properties

        #region Constructors
        internal UserSelectionTabViewModel()
        {
            Debug.WriteLine("## UserSelectionTabView Constructor called");
        }
        #endregion //Constructors

        #region Commands

        public ICommand ToggleLoginCommand
        {
            get
            {
                return Commands.GetOrCreateCommand(
                    () => this.ToggleLoginCommand,
                    (parameter) => DoToggleLogin(parameter) ,
                    () => true);
            }
        }

        private void DoToggleLogin(object parameter)
        {
            if (!IsLoggedOn)
            {
                tracker.Login();
            }
            else
            {
                tracker.Logoff();
            }

            NotifyPropertyChanged("IsLoggedOn");
            NotifyPropertyChanged("LogonButtonText");
            NotifyPropertyChanged("LoginMessage");
        }

        public ICommand ClearFieldsCommand
        {
            get
            {
                return Commands.GetOrCreateCommand(
                    () => this.ClearFieldsCommand,
                    (parameter) => DoClearFields(parameter) ,
                    () => true);
            }
        }
        private void DoClearFields(object parameter)
        {
            UserName = "";
            //TODO:  clear password box too
        }

        public ICommand AddNewUserCommand
        {
            get
            {
                return Commands.GetOrCreateCommand(
                    () => this.AddNewUserCommand,
                    (parameter) => DoAddNewUserCommand(parameter),
                    () => true);
            }
        }
        private void DoAddNewUserCommand(object parameter)
        {
            MetaTracker.View.AddUserWindow addUser = new View.AddUserWindow();
            addUser.ShowDialog();
            addUser.Close();  //Should already be closed, but double check.

            PasswordBox test = new PasswordBox();
        }

        #endregion //Commands

        #region Methods
        #endregion //Methods
    }
}
